/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.auction.rest;
import java.util.Arrays;
import co.yixiang.dozer.service.IGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.auction.domain.Authentication;
import co.yixiang.modules.auction.service.AuthenticationService;
import co.yixiang.modules.auction.service.dto.AuthenticationQueryCriteria;
import co.yixiang.modules.auction.service.dto.AuthenticationDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @author yhh
* @date 2021-02-20
*/
@AllArgsConstructor
@Api(tags = "会员认证审核管理")
@RestController
@RequestMapping("/api/Authentication")
public class AuthenticationController {

    private final AuthenticationService AuthenticationService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','Authentication:list')")
    public void download(HttpServletResponse response, AuthenticationQueryCriteria criteria) throws IOException {
        AuthenticationService.download(generator.convert(AuthenticationService.queryAll(criteria), AuthenticationDto.class), response);
    }

    @GetMapping
    @Log("查询会员认证审核")
    @ApiOperation("查询会员认证审核")
    @PreAuthorize("@el.check('admin','Authentication:list')")
    public ResponseEntity<Object> getAuthentications(AuthenticationQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(AuthenticationService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增会员认证审核")
    @ApiOperation("新增会员认证审核")
    @PreAuthorize("@el.check('admin','Authentication:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Authentication resources){
        return new ResponseEntity<>(AuthenticationService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改会员认证审核")
    @ApiOperation("修改会员认证审核")
    @PreAuthorize("@el.check('admin','Authentication:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Authentication resources){
        AuthenticationService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除会员认证审核")
    @ApiOperation("删除会员认证审核")
    @PreAuthorize("@el.check('admin','Authentication:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        Arrays.asList(ids).forEach(id->{
            AuthenticationService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("会员认证审核")
    @ApiOperation("会员认证审核")
    @PostMapping("/auditStatus")
    public ResponseEntity<Object> auditStatus(@RequestBody Authentication resources) {
        AuthenticationService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Log("获取用户列表")
    @ApiOperation("获取用户列表")
    @GetMapping("/userList")
    public ResponseEntity<Object> userList() {
        List<Authentication> list = AuthenticationService.list(new QueryWrapper<Authentication>().lambda()
                .eq(Authentication::getStatus, 1));
        return new ResponseEntity<>(list , HttpStatus.OK);
    }

}
